package com.vm.osm.business.controller;

import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.common.ErrCode;
import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dto.req.ReqUpdateEquType;
import com.vm.osm.business.dto.req.failType.ReqAddFailType;
import com.vm.osm.business.dto.req.knowledgeBase.ReqAddKnowledge;
import com.vm.osm.business.dto.req.knowledgeType.ReqUpdateTypeInfo;
import com.vm.osm.business.dto.rsp.RspError;
import com.vm.osm.business.dto.rsp.failType.RspFailType;
import com.vm.osm.business.dto.rsp.knowledgeBase.RspKnowledgePage;
import com.vm.osm.business.entity.EamEquType;
import com.vm.osm.business.entity.KnowledgeBase;
import com.vm.osm.business.entity.OsmFailureType;
import com.vm.osm.business.service.IFailTureService;
import com.vm.osm.business.service.IKnowledgeService;
import com.vm.osm.business.util.UserDataUtil;
import com.vm.osm.business.util.UuidUtil;
import com.vma.eams.spring.boot.annotation.LogFunc;
import com.vma.eams.spring.boot.annotation.LogModule;
import com.vma.eams.spring.boot.log.LogCollector;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2018/1/23.
 */
@RestController
@RequestMapping("/v1.0")
@LogModule(type = "failType",name = "故障管理")
@Api(value = "OsmFailTureTypeController",description = "故障类型",tags = {"OsmFailTureTypeController"})
public class OsmFailureTypeController extends ControllerExceptionHandler {
    private Logger LOG = Logger.getLogger(this.getClass());

    @Autowired
    private IFailTureService failTureService;

    /**
     * 新增故障类型
     * @return
     */
    @LogFunc(type = "create",name = "新增故障类型")
    @ApiOperation(value = "新增故障类型",notes = "新增故障类型")
    @ApiImplicitParam(name = "reqAddFailType",value = "新增故障类型",required = true,dataType = "ReqAddFailType")
    @RequestMapping(value = "/add_fail_type",method = RequestMethod.POST)
    public void save(@RequestBody ReqAddFailType reqAddFailType,HttpServletRequest request){
        LOG.info("新增故障类型");
        OsmFailureType failureType = new OsmFailureType();
        failureType.setFailureName(reqAddFailType.getFail_name());
        failureType.setParentId(reqAddFailType.getParent_id());
        failureType.setCreateTime(new Date());
        failureType.setUpdateTime(new Date());
        failureType.setOperatorId(UserDataUtil.getStaffId(request));
        failureType.setStatus(new Byte("1"));
        failureType.setFailCode(reqAddFailType.getCode());
        failureType.setId(UuidUtil.getRandomUuid());
        failureType.setEnterpriseId(UserDataUtil.getUserSelectedEnterpriseId(request));
        int res = failTureService.insert(failureType);
        if (res == 0){
            throw new BusinessException(ErrCode.ADD_DATA_FAIL.toString());
        }
        LogCollector.INSTANCE.put("故障类型",failureType.getFailureName());
    }

    /**
     * 删除故障类型
     * @param id
     * @return
     */
    @LogFunc(type = "delete",name = "删除故障类型")
    @ApiOperation(value = "删除故障类型",notes = "删除故障类型")
    @RequestMapping(value = "/del_fail_type/{id}",method = RequestMethod.DELETE)
    public void deleteFailType(@ApiParam(required=true, name="id", value="故障类型ID") @PathVariable("id") String id){
        LOG.info("删除故障类型");

        OsmFailureType type = failTureService.selectByPrimaryKey(id);

        String typeName = type.getFailureName();

        int res = failTureService.deleteByPrimaryKey(id);
        if (res ==0){
            throw new BusinessException(ErrCode.DELETE_DATA_FAIL.toString());
        }

        LogCollector.INSTANCE.put("故障类型",typeName);
    }

    /**
     * 修改故障类型
     * @param reqUpdateTypeInfo
     * @return
     */
    @LogFunc(type = "update",name = "修改故障类型")
    @ApiOperation(value = "修改故障类型",notes = "修改故障类型")
    @RequestMapping(value = "/update_fail_type_info",method = RequestMethod.PUT)
    public void updateFailTypeInfo(@RequestBody ReqUpdateTypeInfo reqUpdateTypeInfo){
        LOG.info("修改设备类型信息");
        OsmFailureType osmFailureType = failTureService.selectByPrimaryKey(reqUpdateTypeInfo.getId());
        if (osmFailureType == null){
            throw new BusinessException(ErrCode.DATA_NOT_FOUND.toString());
        }
        osmFailureType.setFailureName(reqUpdateTypeInfo.getType_name());
        osmFailureType.setFailCode(reqUpdateTypeInfo.getCode());
        int res = failTureService.updateByPrimaryKeySelective(osmFailureType);
        if (res == 0){
            throw new BusinessException(ErrCode.UPDATE_DATA_FAIL.toString());
        }
        LogCollector.INSTANCE.put("故障类型",osmFailureType.getFailureName());

    }

    @ApiOperation(value = "获取故障类型树",notes = "获取故障类型树")
    @RequestMapping(value = "/get_fail_types",method = RequestMethod.GET)
    public List<RspFailType> getFailTypes(HttpServletRequest request){
        return failTureService.findList("0", UserDataUtil.getUserSelectedEnterpriseId(request));
    }





}
